package cn.xiaoyu.android.activity.index;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import cn.xiaoyu.android.R;
import cn.xiaoyu.android.activity.index.adapter.DividerItemDecoration;
import cn.xiaoyu.android.activity.index.adapter.RecyclerViewAdapter;
import cn.xiaoyu.android.activity.index.entity.ItemEntity;

/**
 * Author: fengzhe
 * Date: 2019/3/15
 * Email: fengzhe@sis.sh.cn
 * Description:
 */
public class IndexActivity extends AppCompatActivity {

    private RecyclerView recyclerView;

    private ArrayList<ItemEntity> mData;

    private RecyclerViewAdapter recycleAdapter = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_launcher);
        recyclerView = findViewById(R.id.rv_list);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        // 设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        // 设置为垂直布局，这也是默认的
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
        // 设置分隔线
        recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        // 设置增加或删除条目的动画
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        initFunctionListData();
    }

    private void initFunctionListData() {
        PackageManager pm = getPackageManager();
        Intent intent = new Intent("launcher");
        intent.addCategory("category.launcher");
        List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, PackageManager.MATCH_ALL);
        mData = new ArrayList<>();
        for (ResolveInfo resolveInfo : resolveInfos) {
            ItemEntity entity = new ItemEntity();
            ActivityInfo activityInfo = resolveInfo.activityInfo;
            try {
                Resources res = pm.getResourcesForApplication(activityInfo.packageName);
                entity.setPackageName(activityInfo.packageName);
                entity.setClazz(activityInfo.name);
                entity.setLabel(activityInfo.labelRes != -1 ? res.getString(activityInfo.labelRes) : "*");
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
            mData.add(entity);
        }
        recycleAdapter = new RecyclerViewAdapter(mData, new RecyclerViewAdapter.OnItemClickedListener() {
            @Override
            public void onItemClicked(int position) {
                Intent intent = new Intent();
                String packageName = mData.get(position).getPackageName();
                String clazz = mData.get(position).getClazz();
                intent.setClassName(packageName, clazz);
                startActivity(intent);
            }
        });
        // 设置Adapter
        recyclerView.setAdapter(recycleAdapter);
    }
}
